package com.lcc.forum.demos.web.services.impl;

import com.lcc.forum.demos.web.common.AppResult;
import com.lcc.forum.demos.web.common.ResultCode;
import com.lcc.forum.demos.web.dao.MessageMapper;
import com.lcc.forum.demos.web.exception.ApplicationException;
import com.lcc.forum.demos.web.model.Message;
import com.lcc.forum.demos.web.model.User;
import com.lcc.forum.demos.web.services.IMessageService;
import com.lcc.forum.demos.web.services.IUserService;
import com.lcc.forum.demos.web.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private IUserService userService;
    @Override
    public void create(Message message) {
        //参数校验
        if(message == null || message.getPostUserId() == null || message.getReceiveUserId() == null
                || StringUtil.isEmpty(message.getContent())){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接受有用户是否存在
        User user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() == 1){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        message.setState((byte) 0);//0表示未读，1表示已读，2表示已回复
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        //调用DAO
        int row = messageMapper.insertSelective(message);
        //校验是否操作成功
        if (row != 1) {
            //打印日志
            log.warn(ResultCode.FAILED_CREATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //1.参数校验
        if (receiveUserId == null || receiveUserId <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用DAO
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        //3.判断count的值是否合法--->正常情况下是不会出现null的
        if (count == null){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //4.返回结果
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //参数合法性校验
        if (receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验当前用户是否存在
        User user = userService.selectById(receiveUserId);
        if (user == null || user.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //调用DAO--->不需要校验，返回给前端进行校验，谁用谁校验
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        //返回结果
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        //参数校验
        //state:0未读 1已读 2已回复 默认为0
        if (id == null || id <= 0 || state < 0 || state > 2){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //构造更新的对象
        Message updateMeesage = new Message();
        updateMeesage.setId(id);
        updateMeesage.setState(state);
        Date date = new Date();
        updateMeesage.setUpdateTime(date);
        //调用DAO
        int row = messageMapper.updateByPrimaryKeySelective(updateMeesage);
        //校验操作是否成功
        if (row != 1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Message selectById(Long id) {
        //校验参数的有效性
        if (id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        Message message = messageMapper.selectByPrimaryKey(id);
        //返回结果
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //校验参数的合法性
        if (repliedId == null || repliedId <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验repliedId对应的站内信是否存在
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        if (existMessage == null || existMessage.getDeleteState() == 1){
            //打印日志
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //将站内信的状态更新为已读
        updateStateById(repliedId, (byte) 2);
        //回复内容写入数据库：新增一条站内信记录
        create(message);
    }
}
